// pages/add_adress/add_adress.js
var amapFile = require('../../amap-wx.js'); 
var tcity = require("../../citys.js");
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    provinces: [],
    province:"",
    citys: [],
    city:"",
    countys: [],
    county:"",
    address:"",
    wran_text:"",
    choose_text: '',
    details_text:'',
    ischooseStyle:'color:#888;',
    value: [0, 0, 0],
    values: [0, 0, 0],
    animationData:{},
    formData:{},
    showTopTips:false,
    condition:true,
    show_choose_page:true,
    address_list:[],
    ischoose:false,
    show_search:false,
    err:0
  },
  open:function(e){
    var agree = e.currentTarget.dataset.agree
    if(agree){
      this.setData({
        address: this.data.province + this.data.city + this.data.county
      })
    }
    this.animate.bottom(-300).step()
    this.setData({
      condition:true,
      animationData: this.animate.export()
    })
  },
  choose_city:function(){
    this.animate.bottom(0).step()
    this.setData({
      condition:false,
      animationData: this.animate.export()
    })
  },
  open_choose_page:function(){
    this.setData({
      show_choose_page:false
    })
  },
  cancel:function(){
    this.setData({
      show_choose_page: true
    })
  },
  get_address:function(e){
    var that = this
    var keywords = e.detail.value;
    this.keywords = e.detail.value
    var myAmapFun = new amapFile.AMapWX({ key: '01a4d722c52ba69ce26a8214feea6d11' });
    myAmapFun.getInputtips({
      keywords: keywords,
      city:"昆明",
      success: function (data) {
        if (data && data.tips) {
          that.setData({
            address_list:data.tips,
            show_search: Boolean(keywords)
          })
        }
      }
    })
  },
  search:function(){
      var that = this
      var myAmapFun = new amapFile.AMapWX({ key: '01a4d722c52ba69ce26a8214feea6d11' });
      var keywords = this.keywords;
      myAmapFun.getInputtips({
        keywords: keywords,
        city: "昆明",
        success: function (data) {
          if (data && data.tips) {
            that.setData({
              address_list: data.tips,
            })
          }
        }
      })
  },
  choose_data:function(e){
    console.log(e)
    // var name = e.currentTarget.dataset.name
    var details_text = e.currentTarget.dataset.address + e.currentTarget.dataset.name
    // var formData = this.data.formData
    // formData.address = address
    this.setData({
      // choose_text: name,
      details_text: details_text,
      show_choose_page:true,
      ischoose:true
    })
  },
  commit_address:function(e){
    var phone = e.detail.value.phone
    var name = e.detail.value.name
    var details = e.detail.value.details
    var data = e.detail.value
    var that = this
    wx.createSelectorQuery().select('#address_text').boundingClientRect(function(msg){
      var address = msg.dataset.value
      console.log(address)
      if(name==""){
        that.showtip("收货人不能为空")
        that.setData({
          err:1
        })
      }else if (phone == "") {
        that.showtip("联系电话不能为空")
        that.setData({
          err: 2
        })
      }else if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))){
        that.showtip("电话号码有误，请重新填写")
        that.setData({
          err: 2
        })
      } else if (that.data.ischoose == false) {
        that.showtip("店铺地址不能为空")
        that.setData({
          err: 3
        })
      } else if (details == "") {
        that.showtip("详细地址不能不能为空")
        that.setData({
          err: 4
        })
      }else{
        var address_id = that.data.formData.address_id
        if (address_id){
          data["address_id"] = address_id
        }
        data['address'] = address
        app.ajax({
          url: app.host + "/mall/user/addaddress",
          data: data,
          success:function(res){
            wx.showToast({
              title: '保存成功',
            })
            setTimeout(function(){
              wx.navigateBack()
            },500)
          }
        })
      }
    }).exec()
  },
  showtip:function(text){
    var that = this
    this.setData({
      wran_text:text,
      showTopTips: true
    })
    setTimeout(function () {
      that.setData({
        showTopTips: false
      });
    }, 2000);
  },
  bindChange: function (e) {
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (options.address_id){
      wx.showLoading({
        title: '加载中',
      })
      app.ajax({
        url: app.host + "mall/user/getaddress?address_id=" + options.address_id,
        success:function(res){
          wx.hideLoading()
          that.setData({
            formData:res.data.data,
            ischoose:true
          })
          console.log(that.data.formData)
        }
      })
    }else{
      app.ajax({
        url: app.host + 'mall/user/shop_details_commit',
        success:function(res){
          var formData = {}
          formData.shop_name = res.data.data.shop_name
          that.setData({
            formData: formData
          })
        }
      })
    }
    tcity.init(that);
    var cityData = that.data.cityData;
    const provinces = [];
    const citys = [];
    const countys = [];
    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }
    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    var animate = wx.createAnimation({
      duration: 200
    })
    this.animate = animate
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})